package com.bingo.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.bingo.demo.pojo.Album;
import com.bingo.demo.pojo.User;
import com.bingo.demo.service.AlbumService;
import com.bingo.demo.utils.uploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class AlbumController {

    @Autowired
    AlbumService albumService;

    @RequestMapping("uploadAlbum.action")
    @ResponseBody
    public JSONObject uploadAlbum(HttpServletRequest request){
        JSONObject json = new JSONObject();
        String imgUrl = request.getParameter("imgUrl");
        String imgMessage = request.getParameter("imgMessage");
        String userId = request.getParameter("userId");
        Album album = new Album();
        if(imgUrl.equals("") ){
            imgUrl = "img/empty.jpg";
        }
        album.setImgUrl(imgUrl);
        album.setImgMessage(imgMessage);
        album.setUserId(Integer.parseInt(userId));
        albumService.addAlbum(album);
        json.put("code",0);
        json.put("msg","发表成功,刷新页面后生效");
        return json;
    }

    @RequestMapping("toAlbum.action")
    public String toAlbum(HttpSession session, Model model){
        if(session.getAttribute("loginUser") == null){
            model.addAttribute("msg","您还没有登录,请先登录");
            return "login";
        }
        User loginUser = (User) session.getAttribute("loginUser");
        List<Album> albumList = albumService.findByUserId(loginUser.getId());
        if(!albumList.isEmpty()){
            Album mainAlbum = albumList.get(albumList.size()-1);
            session.setAttribute("mainAlbum",mainAlbum.getImgUrl());
            session.setAttribute("albumList",albumList);
        }else {
            session.setAttribute("mainAlbum","img/empty.jpg");
        }
        return "album";
    }

    @RequestMapping("albumDelete.action")
    @ResponseBody
    public JSONObject deleteAlbum(HttpServletRequest request){
        JSONObject json = new JSONObject();
        String albumId = request.getParameter("albumId");
        Integer id = Integer.parseInt(albumId);
        Album album = albumService.findById(id);
        if(album!=null){
            String imgUrl = album.getImgUrl();
            albumService.deleteByAlbumId(id);
            //如果相册图片url不为'空图',则删除七牛云的图片
            if(!imgUrl.equals("img/empty.jpg")){
                //uploadUtil.deletePicture(imgUrl);
                uploadUtil.qiniuDelete(imgUrl);
            }
            json.put("code",0);
            json.put("msg","删除成功,刷新页面后生效");
            return json;
        }
        json.put("code",0);
        json.put("msg","删除成功,刷新页面后生效");
        return json;
    }
}
